﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace QControlCenter.ViewModel
{
	public partial class MainWindowViewModel : QBaseVM
	{

		[ObservableProperty]
		private string currentTime;
		[ObservableProperty]
		private string areaName;
		[ObservableProperty]
		private decimal temperature_in;
		[ObservableProperty]
		private decimal temperature_out;
		[ObservableProperty]
		private decimal humidity;
		[ObservableProperty]
		private decimal co2;



		public DispatcherTimer m_secondTimer;
		public DispatcherTimer m_minuteTimer;

		public MainWindowViewModel()
		{
			m_secondTimer = new DispatcherTimer();
			m_secondTimer.Interval = TimeSpan.FromSeconds(1);
			m_secondTimer.Tick += TimerHandler;
			m_secondTimer.Start();

			m_minuteTimer = new DispatcherTimer();
			m_minuteTimer.Tick += Minute_TimerHandler;
			m_minuteTimer.Interval = TimeSpan.FromMinutes(2);
			m_minuteTimer.Start();

		}

		private void TimerHandler(object? sender, EventArgs e)
		{
			CurrentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

		}
		private void Minute_TimerHandler(object? sender, EventArgs e)
		{
			 var res=PlcManager.GetQMitsubishiPlc(AreaName).GetHomePageData();
			Temperature_in = res.temperature_in;
			Temperature_out = res.temperature_out;
			Humidity = res.humidy;
			Co2= res.co2;

		}



		[RelayCommand]
		public void ChangeAreaLayout(string area)
		{
			if (AreaName == area) return;
			AreaName = area;



		}



		public void InitData()
		{

		}




	}
}
